﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using MyPetHub.Common;
using System.Net;

namespace MyPetHub.Common
{
    public class GlobalExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            try
            {
                string exMsg = context.Exception.ToString();
                MyLogger.Error("全局异常：" + exMsg);
            }
            catch{ }
            finally
            {
                ApiReturnInfo ari = new ApiReturnInfo { code = 1, message = "内部异常", data = "请联系管理员" };
                context.Result = new JsonResult(ari);
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.ExceptionHandled = true;
            }
        }

    }
}
